home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
429_01
/
chess12
/
chessui.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-30
|
14KB
|
604 lines
#include "misc.hpp"
#include "brdsize.hpp"
#include "chess.hpp"
#include "chessui.hpp"
#include "chcharui.hpp"
CHESSUSERIFACE ChessUI;
const char whiteText[] = "White",
blackText[] = "Black";
LOCAL const char *colorText(PIECECOLOR color)
{ return(color == WHITE ? whiteText : blackText); }
// returns piece abbreviation to be displayed in board squares
// for a given piece
LOCAL const char *pieceAbbrev
(
PIECECOLOR color,
PIECETYPE type
)
{
if (color == WHITE)
switch (type)
{
case TYPEKING: return("WK");
case TYPEQUEEN: return("WQ");
case TYPEBISHOP: return("WB");
case TYPEKNIGHT: return("WN");
case TYPEROOK: return("WR");
case TYPEPAWN: return("WP");
}
else
switch (type)
{
case TYPEKING: return("BK");
case TYPEQUEEN: return("BQ");
case TYPEBISHOP: return("BB");
case TYPEKNIGHT: return("BN");
case TYPEROOK: return("BR");
case TYPEPAWN: return("BP");
}
return(""); // dummy return
}
const char kingText[] = " King",
queenText[] = " Queen",
bishopText[] = " Bishop",
knightText[] = " Knight",
rookText[] = " Rook",
pawnText[] = " Pawn";
// fills in two entries in a message textList with color/name of
// a piece
LOCAL void pieceText
(
PIECECOLOR color,
PIECETYPE type,
const char **textList
)
{
*(textList++) = colorText(color);
switch (type)
{
case TYPEKING:
*textList = kingText;
return;
case TYPEQUEEN:
*textList = queenText;
return;
case TYPEBISHOP:
*textList = bishopText;
return;
case TYPEKNIGHT:
*textList = knightText;
return;
case TYPEROOK:
*textList = rookText;
return;
case TYPEPAWN:
*textList = pawnText;
return;
}
}
// base textLists for messages. terminated by null pointers. some
// have null pointer place holders for variable portions of message.
const char *noMemory[] =
{
"Insufficient memory. Press any key to exit:",
(char *) 0
};
const char *checkMated[] =
{
(char *) 0,
" King in checkmate. Press any key to exit:",
(char *) 0
};
const int MATEDCOLORINDEX = 0;
const char *staleMate[] =
{
(char *) 0,
" King in stalemate. Press any key to exit:",
(char *) 0
};
const int STALEMATEDCOLORINDEX = 0;
const char *selectPiece[] =
{
(char *) 0,
" Player: Select piece to move with arrow keys, then hit enter:",
(char *) 0
};
const int SELECTCOLORINDEX = 0;
const char *illegalSelection[] =
{
"That location does not contain one of your pieces."
" Press any key to try again:",
(char *) 0
};
const char *moveIllegal[] =
{
"You cannot move the selected piece to that location."
" Press any key to try again:",
(char *) 0
};
const char *wouldLoseKing[] =
{
"This move would result in your King being taken by the ",
(char *) 0,
(char *) 0,
". Press any key to select another move:",
(char *) 0
};
const int DANGERINDEX = 1;
const char *castleQueenRook[] =
{
"Do you want to castle with Queen's Rook? (Type Y or N):",
(char *) 0
};
const char *castleKingRook[] =
{
"Do you want to castle with King's Rook? (Type Y or N):",
(char *) 0
};
const char *selectDest[] =
{
"Select destination of move with arrow keys, then hit enter:",
(char *) 0
};
const char *promoteToWhat[] =
{
"Do you want your Pawn to be promoted to a Queen, Bishop, "
"Knight or Rook? (Type Q, B, K or R):",
(char *) 0
};
// character key lists
const char promoteOptions[] = "qQbBkKrR";
const PIECETYPE promoteType[] = { TYPEQUEEN, TYPEBISHOP, TYPEKNIGHT,
TYPEROOK };
const char yesNoAnswer[] = "yYnN";
const int YESINDEX = 0;
const int NOINDEX = 1;
// more message text lists
const char *thinking[] =
{
(char *) 0,
" Player thinking, please wait...",
(char *) 0
};
const int THINKINGCOLORINDEX = 0;
const char *doCastleQueenRook[] =
{
(char *) 0,
" Player castles with Queen's Rook. Press any key to continue:",
(char *) 0
};
const char *doCastleKingRook[] =
{
(char *) 0,
" Player castles with King's Rook. Press any key to continue:",
(char *) 0
};
const int CASTLECOLORINDEX = 0;
const char *whatMoved[] =
{
(char *) 0,
" Player moves ",
(char *) 0,
(char *) 0,
". Press any key to continue:",
(char *) 0
};
const int MOVECOLORINDEX = 0;
const int MOVEPIECEINDEX = 2;
const char *whatMovedCaptured[] =
{
(char *) 0,
" Player moves ",
(char *) 0,
(char *) 0,
", capturing ",
(char *) 0,
(char *) 0,
". Press any key to continue:",
(char *) 0
};
const int CAPTURINGCOLORINDEX = 0;
const int CAPTURINGPIECEINDEX = 2;
const int CAPTUREDPIECEINDEX = 5;
const char *doPromotion[] =
{
(char *) 0,
" Player promotes Pawn to ",
(char *) 0,
(char *) 0,
". Press any key to continue:",
(char *) 0
};
const int PROMOTECOLORINDEX = 0;
const int PROMOTEPIECEINDEX = 2;
void CHESSUSERIFACE::outOfMemory(void)
{
ChessCharUI.showMessage(noMemory, (char *) 0, (uint *) 0);
return;
}
void CHESSUSERIFACE::init(const BOARD &board)
{
int row, col;
PIECE *p;
ChessCharUI.initScreen();
for (row = 0; row < NUMROWS; row++)
for (col = 0; col < NUMCOLS; col++)
{
p = board.whatPiece(row, col);
if (p)
ChessCharUI.showPiece
(
POSITION(row, col),
pieceAbbrev(p->whatColor(), p->whatType())
);
}
return;
}
void CHESSUSERIFACE::mated(PIECECOLOR color)
{
checkMated[MATEDCOLORINDEX] = colorText(color);
ChessCharUI.showMessage(checkMated, (char *) 0, (uint *) 0);
return;
}
void CHESSUSERIFACE::staleMated(PIECECOLOR color)
{
staleMate[STALEMATEDCOLORINDEX] = colorText(color);
ChessCharUI.showMessage(staleMate, (char *) 0, (uint *) 0);
return;
}
BOOL CHESSUSERIFACE::userMove(BOARD &board, PIECECOLOR color)
{
POSITION start, end;
PIECE *p;
uint keyIndex;
MOVESTATUS moveStatus;
MOVEUNDODATA dummy;
selectPiece[SELECTCOLORINDEX] = colorText(color);
// loop to get legal move
for ( ; ; )
{
// loop to get piece to move
for ( ; ; )
{
start.row = start.col = 0;
if (!ChessCharUI.selectPosition(selectPiece, start))
return(FALSE);
p = board.whatPiece(start);
if (p)
if (p->whatColor() == color)
break;
if (!ChessCharUI.showMessage(illegalSelection,
(char *) 0, (uint *) 0))
return(FALSE);
ChessCharUI.clearSelect(start);
}
if (board.userCanCastle(QUEENSIDECASTLE, color))
if ((p->whatType() == TYPEKING) ||
((p->whatType() == TYPEROOK) && (start.row == 0)))
{
if (!ChessCharUI.showMessage(castleQueenRook,
yesNoAnswer, &keyIndex))
return(FALSE);
if ((keyIndex / 2) == YESINDEX)
{
ChessCharUI.clearSelect(start);
board.castle(QUEENSIDECASTLE, color, dummy);
ChessCharUI.clearPiece(POSITION(0, start.col));
ChessCharUI.showPiece
(
POSITION(3, start.col),
pieceAbbrev(color, TYPEROOK)
);
ChessCharUI.clearPiece(POSITION(4, start.col));
ChessCharUI.showPiece
(
POSITION(2, start.col),
pieceAbbrev(color, TYPEKING)
);
return(TRUE);
}
}
if (board.userCanCastle(KINGSIDECASTLE, color))
if ((p->whatType() == TYPEKING) ||
((p->whatType() == TYPEROOK) && (start.row == 7)))
{
if (!ChessCharUI.showMessage(castleKingRook,
yesNoAnswer, &keyIndex))
return(FALSE);
if ((keyIndex / 2) == YESINDEX)
{
ChessCharUI.clearSelect(start);
board.castle(KINGSIDECASTLE, color, dummy);
ChessCharUI.clearPiece(POSITION(7, start.co